// pages/user/historical/historical.js
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify'
const date = require('../../utils/Date.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {

    invoiceList: '',
    name: 0,
    active: 0,
    code: '',
    count: 0,
    mobile: '',
    phone: 0,
    tabflag:0,
    pageNo: 0
  },

  gotitledeatil: function (e) {

    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../details/details?id=' + id,
    })
  },
  onClick: function (event) {

    var that = this;
    that.setData({
      active: event.detail.name
    })
    wx.request({
      url: 'https://gunku.efficientgolf.net/backend/wxapi/list',
      data: {
        mobile: that.data.phone,
        type: event.detail.name,
        pageNo: 1,
        pageSize: 5
      },
      method: "get",
      success: function (res) {
        if(res.data.result===null||res.data.result.length==0)
        {
          that.setData({
            tabflag:1
          })
        }else{
          that.setData({
            tabflag:0
          })
        }
 
        that.setData({
          pageNo: res.data.result.pageNo
        })
        if (event.detail.name == "4") {
          that.setData({
            list: res.data.result,
            title: res.data.result.typeMap,
            lab: res.data.result.tagMap,
            count: (1 / (res.data.result.programme + res.data.result.resolved + res.data.result.unresolved)).toFixed(2)
          })
          console.log(that.data.count)
        } else if (event.detail.name == "5") { 
          for(var i=0;i<res.data.result.length;i++){
          res.data.result[i].createTime = date.formatDate(res.data.result[i].createTime).substring(0, 10)
        }
          console.log(res.data.result)
          that.setData({
            pdf:res.data.result
          })
        }
        else {
          that.setData({
            list: res.data.result.data,
            length:1
          })
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */


  getPhoneNumber: function (e) {

    var that = this;

    if (e.detail.errMsg == "getPhoneNumber:ok") {
      wx.request({
        url: 'https://gunku.efficientgolf.net/backend/wxapi/login',
        data: {
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv,
          code: that.data.code
        },
        method: "get",
        success: function (res) {
          if(!res.data.success) {
            console.log(res)
            Notify({ type: 'danger', message: res.data.message })
          }else {
            that.setData({
              phone: res.data.result.phone,
              flage: 0
            })
  
            wx.request({
              url: 'https://gunku.efficientgolf.net/backend/wxapi/list',
              data: {
                mobile: res.data.result.phone,
                type: 0,
                pageNo: 1,
                pageSize: 5
              },
              method: "get",
              success: function (res) {
  
                that.setData({
                  list: res.data.result.data,
                  pageNo:res.data.result.pageNo,
                  length:1
                })
  
              }
            })
            wx.setStorage({
              key: 'mobile',
              data: res.data.result.phone
            })
          }
          
        }
      })
    }

  },
  pdfX:function(e){
    var file=e.currentTarget.dataset.url

    wx.downloadFile({
      url: file,      //要预览的PDF的地址
      success: function (res) {                           
        console.log(res);
        if (res.statusCode === 200) {                     //成功
          var Path = res.tempFilePath                     //返回的文件临时地址，用于后面打开本地预览所用
          wx.openDocument({
            filePath: Path,                               //要打开的文件路径
            success: function (res) {
              console.log('打开PDF成功');
            }
          })
        }
      },
      fail: function (res) {
        console.log(res);                                  //失败
      }
  })
  },
  onLoad: function (options) {
  


    var that = this;
    var mobile = '';

    wx.getStorage({
      key: 'mobile',
      success: function (res) {
        that.setData({
          phone: res.data,
        })
        wx.request({
          url: 'https://gunku.efficientgolf.net/backend/wxapi/list',
          data: {
            mobile: res.data,
            type: that.data.name,
            pageNo: 1,
            pageSize: 5
          },
          method: "get",
          success: function (res) {
            if(res.data.result===null)
            {
              that.setData({
                tabflag:1
              })
            }else{
              that.setData({
                tabflag:0
              })
            }

            
            console.log(res.data.result.data)
            that.setData({
              list: res.data.result.data,
              pageNo: res.data.result.pageNo,
              length:1
            })
          }
        })

      },
      fail: function (res) {
        that.setData({
          flage: 1
        })
      }
    })


    wx.login({
      success(res) {
        console.log(res);
        var code = res.code
        that.setData({
          code: res.code
        })

      }
    })







  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    
    wx.request({
      url: 'https://gunku.efficientgolf.net/backend/wxapi/list',
      data: {
        mobile: this.data.phone,
        type: this.data.active,
        pageNo: this.data.pageNo + 1,
        pageSize:5
      },
      method: "get",
      
      success: function (res) {
        if (res.data.result.length != 0) {
          that.setData({
            list: res.data.result.data,
            pageNo: res.data.result.pageNo,
            length:1
          })
        }
        else{
            console.log(res.data.result.length)
          that.setData({
            length:res.data.result.length
          })
        }
      }
    })

  },
  
  retindex(){
    var that = this
    
    wx.request({
      url: 'https://gunku.efficientgolf.net/backend/wxapi/list',
      data: {
        mobile: this.data.phone,
        type: this.data.active,
        pageNo: 1,
        pageSize: 5
      },
      method: "get",
      success: function (res) {
        wx.pageScrollTo({
          scrollTop: 0
        })
          that.setData({
            list: res.data.result.data,
            pageNo: res.data.result.pageNo,
            length:1
          })
        
      }
    })

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})